package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.Link;
import cn.xbtop.blog.service.LinkService;
import cn.xbtop.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 友情链接管理
 *
 * @author Mxb
 * @version 1.0
 * @date 2020/4/11 14:10
 */
@RestController
@RequestMapping("/link")
public class LinkController {
    @Autowired
    private LinkService linkService;

    /**
     * 添加
     *
     * @param link
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Link link) {
        linkService.insert(link);
        return new Result<>("添加成功！");
    }

    /**
     * 修改
     *
     * @param link
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Link link) {
        linkService.updateByPrimaryKey(link);
        return new Result<>("修改成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result<Object> delete(@PathVariable Integer id) {
        linkService.deleteByPrimaryKey(id);

        return new Result<>("删除成功！");
    }

    /**
     * 查询所有
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<Object> list() {
        List<Link> list = linkService.findAll();
        return new Result<>(list);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result<Object> findById(@PathVariable Integer id) {
        Link link = linkService.selectByPrimaryKey(id);
        return new Result<>(link);
    }
}
